package com.ss.android.article.browser.c;

import android.app.Application;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcel;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public final class d {
    public static final String DEFAULT_DOWNLOAD_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

    public static long a() {
        return 1048576;
    }

    @NonNull
    public static String readInputStream(@NonNull InputStream inputStream, @NonNull String charsetName) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charsetName));
        StringBuilder stringBuilder = new StringBuilder();
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            stringBuilder.append(line);
        }
        return stringBuilder.toString();
    }

    public static void deleteFile(@NonNull Application application, @NonNull String name) {
        File file = new File(application.getFilesDir(), name);
        if (file.exists()) {
            file.delete();
        }
    }

    @Nullable
    public static Bundle readBundleFromStorage(@NonNull Application arg8, @NonNull String arg9) {
        Bundle v0_5;
        Closeable v0_3;
        Closeable v2_1;
        FileInputStream v2 = null;
        Bundle v1 = null;
        File v3 = new File(arg8.getFilesDir(), arg9);
        try {
            v2 = new FileInputStream(v3);
            Parcel parcel = Parcel.obtain();
            byte[] v0_4 = new byte[((int) v2.getChannel().size())];
            v2.read(v0_4, 0, v0_4.length);
            parcel.unmarshall(v0_4, 0, v0_4.length);
            parcel.setDataPosition(0);
            v0_5 = parcel.readBundle(ClassLoader.getSystemClassLoader());
            v0_5.putAll(v0_5);
            parcel.recycle();
            v3.delete();
            android.support.design.a.closeQuietly(((Closeable) v2));
            return v0_5;
        } catch (FileNotFoundException fileNotFoundException) {
            Log.e("FileUtils", "Unable to read bundle from storage");
            return v1;
        } catch (IOException v0_1) {
            v0_1.printStackTrace();
        } finally {
            v3.delete();
            android.support.design.a.a(v2);
            return v1;
        }
    }
}

